package algo.sort;

public class Selection<T extends Comparable<? super T>> extends Sort<T> {
    @Override
    public void sort(T[] nums) {
        for (int i=0, lastIndex=nums.length-1; i<lastIndex; ++i) {
            int minIndex = i;
            for (int j=i+1; j<=lastIndex; ++j) {
                if (less(nums[j], nums[minIndex])) {
                    minIndex = j;
                }
            }

            if (minIndex != i) {
                swap(nums, i, minIndex);
            }
        }
    }

    @Override
    public void sortDesc(T[] nums) {
        for (int i=0, lastIndex=nums.length-1; i<lastIndex; ++i) {
            int minIndex = i;
            for (int j=i+1; j<=lastIndex; ++j) {
                if (greater(nums[j], nums[minIndex])) {
                    minIndex = j;
                }
            }

            if (minIndex != i) {
                swap(nums, i, minIndex);
            }
        }
    }
}
